<?php
 
require_once dirname(__FILE__) . '/PgSQL.php';

$hotel = isset($_GET['hotel']) ? $_GET['hotel'] : '';
$district = isset($_GET['district']) ? $_GET['district'] : '';

$queryStr = "";
switch ($district) {
	case 'all':
		$queryStr = 'SELECT name, ST_AsText(the_geom) AS wkt FROM dnhotel WHERE name ILIKE \'%'.$hotel.'%\'';
		break;
	default:
		$queryStr = 'SELECT name, ST_AsText(the_geom) AS wkt FROM dnhotel WHERE name ILIKE \'%'.$hotel.'%\' AND district ILIKE \'%'.$district.'%\'';
}

//echo $queryStr;

if ($queryStr != '') {
	// Khởi tạo đối tượng PgSQL
	$pg = new PgSQL();
	$pg->connect();
	$result = $pg->query($queryStr);
	
	$json = '{"results":[';
	
	while ($row = pg_fetch_object($result)) {
		$json .= '{"geom":"' . $row->wkt . '", "name":"' . $row->name . '"}, ';
	}
	
	// Bỏ hai ký tự cuối bị thừa
	$json = rtrim($json, ', ');
	
	$json .= ']}';
	
	echo $json;
}